package com.android.djlmapp.tools.global;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import java.util.Stack;

/**
 * avtivity管理类
 */

public class MyActivityManager {
    private static MyActivityManager instance;
    public static Stack<Activity> mActivites;

    private MyActivityManager() {
    }

    /**
     * 判断一个Activity 是否存在
     * progressDialog.getOwnerActivity()
     *
     * @return
     */
    public static boolean isActivityExist(Activity activity) {

        if (activity == null || activity.isDestroyed() || activity.isFinishing()) {
            return false;
        } else {
            return true;
        }
//        if (progressDialog != null && progressDialog.isShowing()) {
//            progressDialog.dismiss();
//        }
    }

    /**
     * 单一实例
     */
    public static MyActivityManager getAppManager() {
        if (instance == null) {
            instance = new MyActivityManager();
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public static void addActivity(Activity act) {
        if (mActivites == null) {
            mActivites = new Stack<Activity>();
        }
        mActivites.add(act);
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = mActivites.lastElement();
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    public static void finishActivity(Activity activity) {
        if (activity != null) {
            mActivites.remove(activity); //将已经销毁的activity在list中清除，与实际的activity活动个数保持.一致
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public static void finishActivity(Class<?> cls) {
        for (Activity activity : mActivites) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    /**
     * 判断Activity还在不在
     */
    public static boolean isActivityRun(Class<?> cls) {
        for (Activity activity : mActivites) {
            if (activity.getClass().equals(cls)) {
                return true;
            }
        }
        return true;
    }

    /**
     * 在退出登录时全部关闭
     */
    public static void finishAll() {
        for (Activity act : mActivites) {
            if (!act.isFinishing()) {//这里判断act是否正在销毁，如果没有就调用finish进行销毁
                act.finish();
            }
        }
    }

    /**
     * 退出应用程序
     */
    @SuppressWarnings("deprecation")
    public void AppExit(Context context) {
        try {
            finishAll();
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityManager.restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
